package cn.biplam.back.bean.systemManage.privi;

import java.io.Serializable;
import java.util.ArrayList;

import cn.biplam.common.bean.systemManage.privi.MenuManageBean;
import cn.biplam.common.bean.tree.IndexTableExt;
import cn.biplam.common.bean.tree.IndexTableSimple;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.MenuOptCode;
import cn.biplam.common.paramters.TreeParam.Index_Tree_Node_Type;
import cn.biplam.common.struc.SysStruc.MenuStruc;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  系统菜单管理操作
 * @author XiaoRuxing Dec 8, 20098:18:15 PM
 * @version 1.0
 */
public class MenuMangeBeanImp extends MenuManageBean implements Serializable{

	/**  */
	private static final long serialVersionUID = 102917471028301L;

	public MenuMangeBeanImp(String userId){
		this.userId = userId;
	}

	/**
	 *  创建菜单
	 * @param parentId     父亲节点ID
	 * @param parentOpType 父亲节点操作类型
	 * @param menuName     菜单名称
	 * @param menuDesc     菜单描述
	 * @param menuPicPath  菜单路径
	 * @param menuActionId 菜单跳转动作ID
	 * @return 操作结果:创建是否成功;节点ID
	 * @throws ErrorMessage 
	 */
	@Override
	public OperatorResult insert(String parentId, String parentOpType,
			String menuName, String menuDesc, String menuPicPath,
			String menuActionId) throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		
		//插入菜单到权限表
		//设置当前菜单操作类型
		String opType = null;
		String menuParent = null; 
		if(parentOpType.equals(Index_Tree_Node_Type.ROOT_THEME.toString())){
			opType = Index_Tree_Node_Type.UN_ROOT_THEME.toString();//父菜单操作类型为根节点,则当前菜单类型为一级菜单
			menuParent = "1"; //菜单表的根节点ID为1
		}else if(parentOpType.equals(Index_Tree_Node_Type.UN_ROOT_THEME.toString())){
			opType = Index_Tree_Node_Type.OTHER.toString();//父菜单操作类型为一级菜单,则当前菜单类型为二级菜单
			menuParent = parentId;
		}else{
			throw new ErrorMessage("Menu OperatorType "+parentOpType+" is not allow!");
		}
		
		OperatorResult indexInsert = new PriviManageBeanImp(userId).insertPrivi(parentId, 
				opType, menuName, menuDesc, "1", true);
		String menuId = indexInsert.getLastRowId();//获取菜单ID
		String menuSeq = indexInsert.getValue();//获取菜单排序号
		sql.addAll(indexInsert.getSqls());//获取插入菜单权限的SQL

		//插入菜单到菜单表
		sql.addAll(Operator.get(MenuOptCode.Insert, userId, 
				getParams(menuParent, menuId, menuName, menuDesc, menuPicPath, menuActionId, menuSeq),
				true).getSqls());

		return this.doingDML(sql);
	}

	/**
	 * @return 新建菜单参数容器
	 */
	private DataFilter getParams(String parentId, String menuId,
			String menuName, String menuDesc, String menuPicPath,
			String menuActionId,String menuSeq) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(MenuStruc.MENU_ID, menuId);
		filter.put(MenuStruc.MENU_PARENT, parentId);
		filter.put(MenuStruc.MENU_NAME, menuName);
		filter.put(MenuStruc.MENU_DESC, menuDesc);
		filter.put(MenuStruc.PIC_PATH, menuPicPath);
		filter.put(MenuStruc.ACTION_ID, menuActionId);
		filter.put(MenuStruc.MENU_SEQUENCE, menuSeq);
		return filter;
	}

	/**
	 *  删除菜单
	 * @param menuId 菜单ID
	 * @return 操作结果
	 * @throws ErrorMessage 
	 */
	@Override
	public OperatorResult delete(String menuId) throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		//获取所有下级菜单
		IndexTableExt indexOp = IndexTableExt.getNewInstance(userId, PriviStruc.Table_Name);
		ArrayList<String> subMenus = indexOp.getChilds(menuId, null, null);//下级菜单列表
		//删除所有下级菜单
		for(String subMenu: subMenus){
			deleteMenu(subMenu, sql);
		}
		//删除菜单(级联:删除权限(删除权限和角色权限))
		deleteMenu(menuId, sql);
		return this.doingDML(sql);
	}

	/**
	 *  修改菜单信息
	 * @param menuId        菜单ID
	 * @param menuName      菜单名称
	 * @param menuDesc		菜单描述
	 * @param menuPicPath	菜单图标路径
	 * @param menuActionId  菜单动作ID
	 * @return 操作结果
	 */
	@Override
	public OperatorResult update(String menuId, String menuName,
			String menuDesc, String menuPicPath, String menuActionId) throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		//修改权限表
		IndexTableSimple index = IndexTableSimple.getNewInstance(userId, PriviStruc.Table_Name);
		sql.addAll(index.update(menuId, menuName, menuDesc, null, true).getSqls());

		//修改菜单表
		DataFilter filter = getParams(null, menuId, menuName, menuDesc, menuPicPath, menuActionId, null);
		sql.addAll(Operator.get(MenuOptCode.Update, userId, filter,true).getSqls());

		return this.doingDML(sql);
	}

	/**
	 *  删除单个菜单
	 * @param menuId 菜单ID
	 * @throws ErrorMessage 
	 */
	private void deleteMenu(String menuId,ArrayList<String> sql) throws ErrorMessage{
		//删除菜单表中菜单
		DataFilter filter = new DataFilter(menuId);
		sql.addAll(Operator.get(MenuOptCode.Del, userId, filter,true).getSqls());
		//删除权限表中菜单权限和角色权限
		sql.addAll(new PriviManageBeanImp(userId).delPrivi(menuId, true).getSqls());
	}
}
